{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "kxu1Gfhx1pHg"
   },
   "source": [
    "<a href=\"https://colab.research.google.com/github/jeffheaton/app_generative_ai/blob/main/t81_559_class_12_2_fewshot_cot.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "ZbNbAV281pHh"
   },
   "source": [
    "# T81-559: Applications of Generative Artificial Intelligence\n",
    "**Module 12: Prompt Engineering**\n",
    "* Instructor: [Jeff Heaton](https://sites.wustl.edu/jeffheaton/), McKelvey School of Engineering, [Washington University in St. Louis](https://engineering.wustl.edu/Programs/Pages/default.aspx)\n",
    "* For more information visit the [class website](https://sites.wustl.edu/jeffheaton/t81-558/)."
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "id": "HwnvSYEQ1pHi"
   },
   "source": [
    "# Module 12 Material\n",
    "\n",
    "Module 12: Prompt Engineering\n",
    "\n",
    "* Part 12.1: Intro to Prompt Engineering [[Video]](https://www.youtube.com/watch?v=p8q2kFhyX3M&ab_channel=JeffHeaton) [[Notebook]](t81_559_class_12_1_prompt_engineering)\n",
    "* **Part 12.2: Few Shot and Chain of Thought** [[Video]](https://www.youtube.com/watch?v=Bj-zOiqpp6g&ab_channel=JeffHeaton) [[Notebook]](t81_559_class_12_2_fewshot_cot.ipynb)\n",
    "* Part 12.3: Persona and Role Patterns [[Video]](https://www.youtube.com/watch?v=uD9z3LFw7nA&ab_channel=JeffHeaton) [[Notebook]](t81_559_class_12_3_persona.ipynb)\n",
    "* Part 12.4: Question, Refinement and Verification Patterns [[Video]](https://www.youtube.com/watch?v=UF0gN79hg4g&ab_channel=JeffHeaton) [[Notebook]](t81_559_class_12_4_refinement_eval.ipynb)\n",
    "* Part 12.5: Content Creation and Structured Prompt Patterns [[Video]](https://www.youtube.com/watch?v=PPNnTDfKjPE&ab_channel=JeffHeaton) [[Notebook]](t81_559_class_12_5_create.ipynb)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "v2MPPX0c1pHi"
   },
   "source": [
    "# Part 12.2: Few Shot and Chain of Thought\n",
    "\n",
    "In this module, we explore two powerful prompting techniques that can significantly enhance the accuracy and utility of generative AI in applications like life insurance underwriting: few-shot prompting and chain-of-thought prompting. Both methods address different challenges insurance prompt writers might face, such as evaluating applicant risk, explaining policy options, or estimating premiums by tailoring the AI’s responses to be more thoughtful, accurate, and aligned with industry standards.\n",
    "\n",
    "Few-shot prompting, a powerful technique, empowers the model with a few examples to guide its responses in new situations. This method is particularly efficient when the goal is to generate structured outputs, such as categorizing applicants by risk, summarizing policies, or estimating life expectancy. By providing just a handful of examples, the model learns how to handle similar tasks, making it an efficient way to improve accuracy without overwhelming the model with data. In a life insurance context, few-shot prompting can save underwriters valuable time by ensuring that the AI model produces responses that are well-calibrated to the task at hand, following patterns from the examples.\n",
    "On the other hand, chain of thought prompting provides underwriters with a valuable tool to gain insight into the AI's decision-making process. This technique allows the model to reason step-by-step through a problem, providing transparency into its decision-making process. Instead of simply offering an output, the model walks through its reasoning in a structured manner. This technique is particularly valuable for complex assessments where multiple factors need to be considered, such as evaluating an applicant’s health history, lifestyle, and genetic predispositions when determining risk. By encouraging the AI to articulate each step, underwriters gain insight into how the model reached its conclusion, making the decision process more reliable and explainable.\n",
    "Together, few-shot prompting and chain-of-thought prompting provide underwriters with robust tools to leverage AI in decision-making. Few-shot prompting enables the model to replicate patterns based on a few provided examples, helping in tasks like classification and summarization. In contrast, chain-of-thought prompting ensures that complex reasoning is thorough and transparent. These techniques can complement each other, enhancing the accuracy and interpretability of AI-generated responses in the life insurance field.\n",
    "\n",
    "### Few-Shot Prompting\n",
    "\n",
    "In the context of life insurance, \"few-shot\" prompting can be a powerful tool to fine-tune the performance of generative AI models for specific tasks. Few-shot prompting refers to the technique where the model is given a handful of examples (typically 2-5) that demonstrate the desired output structure before it processes a new input. This helps guide the model toward generating more accurate and contextually appropriate responses based on the examples provided. For life insurance underwriters, few-shot prompting can streamline tasks such as analyzing applicant data, explaining policy options, or estimating risk factors.\n",
    "\n",
    "For example, an underwriter might want the model to classify applicants into different risk categories based on their medical history. A few-shot prompt could look like this:\n",
    "\n",
    "Prompt Example 1:\n",
    "```\n",
    "Classify the following applicants based on their medical history:\n",
    "\n",
    "Applicant 1: Age 45, history of smoking, mild hypertension.\n",
    "Classification: Moderate Risk.\n",
    "\n",
    "Applicant 2: Age 30, non-smoker, no significant medical history.\n",
    "Classification: Low Risk.\n",
    "\n",
    "Applicant 3: Age 55, history of diabetes and heart disease.\n",
    "Classification: High Risk.\n",
    "\n",
    "Applicant 4: Age 50, history of high cholesterol.\n",
    "Classification: \n",
    "```\n",
    "\n",
    "By including several examples of applicants with varying health profiles, the model is primed to understand the relationship between health data and risk categories. The AI can then apply these patterns to new cases, assisting underwriters in identifying risk factors efficiently.\n",
    "\n",
    "In another case, a few-shot prompt can be used to help an AI generate policy explanations. An underwriter might provide examples of simple and clear policy summaries:\n",
    "\n",
    "Prompt Example 2\n",
    "\n",
    "```\n",
    "Summarize these life insurance policies in a single sentence:\n",
    "\n",
    "Policy A: $500,000 coverage, 30-year term, premiums increase after 10 years.\n",
    "Summary: A 30-year term policy with $500,000 coverage, with premiums that increase after the first 10 years.\n",
    "Policy B: $250,000 coverage, whole life policy with guaranteed premiums.\n",
    "Summary: A whole life policy providing $250,000 coverage with guaranteed stable premiums.\n",
    "Policy C: $350,000 coverage, CUL life policy.\n",
    "Summary: \n",
    "```\n",
    "\n",
    "These examples help the model understand how to summarize policy details in a concise and understandable manner, useful for underwriters explaining complex products to clients.\n",
    "\n",
    "A final use case might involve estimating life expectancy based on health factors, which is a crucial part of risk assessment for life insurance policies. A few-shot prompt might include examples like:\n",
    "\n",
    "Prompt Example 3\n",
    "\n",
    "```\n",
    "Estimate the life expectancy for the following individuals based on their health profiles:\n",
    "\n",
    "Person A: Age 60, non-smoker, exercises regularly, no significant medical conditions.\n",
    "Life Expectancy: 85 years.\n",
    "Person B: Age 45, smoker, history of high cholesterol and hypertension.\n",
    "Life Expectancy: 70 years.\n",
    "Person C: Age 35, heavy smoker, history of cancer and diabetes\n",
    "Life Expectancy:\n",
    "```\n",
    "\n",
    "In this case, few-shot prompting provides the model with clear benchmarks for different health scenarios, helping underwriters estimate life expectancy more consistently. Each example in the prompt conditions the model to replicate accurate patterns in its future predictions.\n",
    "\n",
    "By using few-shot prompting in these ways, underwriters can leverage AI to enhance their decision-making processes while ensuring the output aligns with industry standards.\n",
    "\n",
    "### Chain of Thought Prompting\n",
    "\n",
    "Chain of thought prompting is a technique in which the AI model is encouraged to articulate its reasoning step by step before reaching a conclusion. This method helps improve the accuracy of the model's responses by making its thought process more transparent and logical. In a life insurance context, chain of thought prompting can be particularly useful for complex tasks like risk assessment, policy recommendations, or premium calculations, where the reasoning behind a decision is just as important as the conclusion itself.\n",
    "\n",
    "When an underwriter asks the model to assess a potential client's risk based on multiple factors such as age, health history, lifestyle, and family medical history, using a chain of thought prompt allows the model to break down each consideration systematically. This ensures that no crucial detail is overlooked and the final decision is well-reasoned.\n",
    "\n",
    "```\n",
    "Prompt:\n",
    "Assess the risk level for a 52-year-old male applicant who has a history of hypertension, is a non-smoker, exercises regularly, and has a family history of heart disease. Please explain your reasoning step by step.\n",
    "\n",
    "Model Response:\n",
    "Let’s break down the risk assessment step by step:\n",
    "\n",
    "Age: The applicant is 52 years old, which places him in the middle-aged category. While age alone does not immediately indicate high risk, it is a factor that needs to be considered, as health risks tend to increase with age.\n",
    "\n",
    "Health history: The applicant has a history of hypertension. This condition is a significant risk factor for heart disease, stroke, and other cardiovascular problems. However, the severity of the hypertension and how well it is managed (e.g., through medication or lifestyle changes) would further influence this risk.\n",
    "\n",
    "Lifestyle: The applicant is a non-smoker, which significantly reduces the risk of heart disease, lung disease, and cancer. Additionally, the fact that he exercises regularly is a positive factor, as regular physical activity helps control blood pressure, manage weight, and improve overall cardiovascular health. This healthy lifestyle choice can partially mitigate the risks posed by his hypertension.\n",
    "\n",
    "Family medical history: The applicant has a family history of heart disease. This is an important consideration because it suggests a genetic predisposition to cardiovascular issues. Even though the applicant’s own lifestyle choices are healthy, this hereditary factor could increase his long-term risk.\n",
    "\n",
    "Final assessment: Taking all factors into account, the applicant is not in the highest risk category due to his non-smoking status and regular exercise. However, his history of hypertension and family predisposition to heart disease moderately increase his risk. Therefore, I would classify him as moderate risk, with the recommendation to further monitor his blood pressure management and cardiovascular health over time.\n",
    "```\n",
    "\n",
    "In this example, the chain of thought prompting allows the AI to articulate its reasoning process clearly, considering each factor separately before arriving at the final risk classification. This approach helps ensure that underwriters can follow the AI's logic, providing transparency and a more structured decision-making process.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "colab": {
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
